//
//  THEmptyView.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THEmptyView.h"
@interface THEmptyView ()

@property (nonatomic,retain) UIView *containerView;

@end

@implementation THEmptyView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addOwnView];
    }
    return self;
}
- (void)addOwnView{
    [self addSubview:self.containerView];
    [self.containerView addSubview:self.imageView];
    [self.containerView addSubview:self.textLable];
    [self.containerView addSubview:self.relaodButton];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(0);
    }];
    
    [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.containerView);
        make.width.mas_equalTo(kSizeRateTo(140));
        make.height.mas_equalTo(kSizeRateTo(140));
        make.top.equalTo(0);
    }];
    
    [self.textLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.containerView);
        make.top.equalTo(self.imageView.mas_bottom).offset(kSizeRateTo(30));
        make.left.right.equalTo(0);
        make.width.equalTo(kSizeRateTo(290));
    }];
    
    [self.relaodButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.containerView);
        make.width.mas_equalTo(kSizeRateTo(244));
        make.height.mas_equalTo(kSizeRateTo(50));
        make.top.equalTo(self.textLable.mas_bottom).offset(kSizeRateTo(30));
        make.bottom.equalTo(0);
    }];
    
    _relaodButton.layer.cornerRadius  = kSizeRateTo(25);
    _relaodButton.layer.masksToBounds = YES;
    
    if (self.lodingView) {
        [self.lodingView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.containerView);
            make.size.mas_equalTo(CGSizeMake(300, 300));
        }];
    }
}
- (void)showLodingView:(UIView<THEmptyViewAnimateProtocol> *)lodingView{
    [_lodingView removeFromSuperview];
    _lodingView = lodingView;
    [self.containerView addSubview:_lodingView];
    //临时方案,等设计出图 在对loadingView进行修改
    _lodingView.frame = CGRectMake(0, 0, 300, 300);
    _lodingView.alpha = 0.0;
    self.imageView.hidden = YES;
    self.textLable.hidden = YES;
    self.relaodButton.hidden = YES;
    
    [_lodingView startAnimating];
    [self layoutSubviews];
}

- (void)showEmptyText:(NSString*)text{
    if(_lodingView) {
        [_lodingView stopAnimating];
        [_lodingView removeFromSuperview];
        _lodingView = nil;
    }
    self.textLable.text = text;
    self.imageView.hidden = NO;
    self.textLable.hidden = NO;
    [self layoutSubviews];
}
- (void)showEmptyImageName:(NSString*)imageName{
    self.imageView.image = kImage(imageName);
}
- (void)showBtnWithSEL:(SEL)sel target:(id)target{
    [self showBtnWithSEL:sel target:target btnTitle:@"重新加载"];
}
- (void)showBtnWithSEL:(SEL)sel target:(id)target btnTitle:(NSString*)btnTitle{
    [[self.relaodButton allTargets] enumerateObjectsUsingBlock: ^(id object, BOOL *stop) {
        [self.relaodButton removeTarget:object action:NULL forControlEvents:UIControlEventAllEvents];
    }];
    [self.relaodButton addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    [self.relaodButton setTitle:btnTitle forState:UIControlStateNormal];
}
#pragma mark  lazy method
- (UIView*)containerView{
    if (!_containerView) {
        _containerView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return _containerView;
}
- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _imageView.image = kImage(@"baseImg_userData_empty_icon");
    }
    return _imageView;
}
- (UILabel *)textLable{
    if (!_textLable) {
        _textLable = [[UILabel alloc] initWithFrame:CGRectZero];
        _textLable.numberOfLines = 0;
        _textLable.font = kFontRegular(kSizeRateTo(16));
        _textLable.textColor = Color_text_99;
        _textLable.textAlignment = NSTextAlignmentCenter;
//        _textLable.text = @"暂无数据";
        _textLable.numberOfLines = 0;
    }
    return _textLable;
}
- (UIButton*)relaodButton{
    if (!_relaodButton) {
        _relaodButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_relaodButton setTitle:@"重新加载" forState:UIControlStateNormal];
        _relaodButton.titleLabel.font = kFontMedium(kSizeRateTo(18));
        [_relaodButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        UIImage *image = [UIImage imageWithColor: kThemeColor size:CGSizeMake(kSizeRateTo(220),kSizeRateTo(50))];
        [_relaodButton setBackgroundImage:image forState:UIControlStateNormal];
        [_relaodButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _relaodButton.hidden = YES;
    }
    return _relaodButton;
}
@end
